package com.example.springtransaction.service;

import com.example.springtransaction.mapper.UserMapper;
import com.example.springtransaction.model.LogInfo;
import com.example.springtransaction.model.Userinfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 23735
 * Date: 2023-03-16
 * Time: 23:09
 */
@Slf4j
@Service
public class UserService {

    @Autowired
    private LogService logService;
    @Autowired
    private UserMapper userMapper;
//    @Transactional
//    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public int add(Userinfo userinfo){
        return userMapper.add1(userinfo);
    }

    @Transactional(propagation = Propagation.NESTED)
    public int save(Userinfo userinfo){
        int result=userMapper.add1(userinfo);
        log.info("添加用户的结果为: "+result);
        LogInfo logInfo=new LogInfo();
        logInfo.setTitle("添加用户");
        logInfo.setContent("添加用户结果为: "+result);
        int logResult=logService.save(logInfo);
        return result;
    }


    @Transactional(propagation = Propagation.NESTED)
    public int save8(Userinfo userinfo){
        userinfo.setName("8");
        return userMapper.add1(userinfo);
    }

    @Transactional(propagation = Propagation.NESTED)
    public int save9(Userinfo userinfo){
        userinfo.setName("9");


        return userMapper.add1(userinfo);
    }


    @Transactional(propagation = Propagation.NESTED)
    public int save10(Userinfo userinfo){
        userinfo.setName("10");
        userMapper.add1(userinfo);
//        try {
//        } catch (Exception e) {
//            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
//        }
//        int val = 10 / 0;
        return -1;
    }


    @Transactional(propagation = Propagation.NESTED)
    public int save11(Userinfo userinfo){
        userinfo.setName("11");

        return userMapper.add1(userinfo);
    }


}
